﻿
using System.Windows;
using System;
namespace Silvery.Role
{
    public enum RoleState
    {
        Wait, Run
    }
    public enum RoleDirection
    {
        Up, UpLeft,Left,LeftDown, Down,RightDown,Right,UpRight 
    }
    /// <summary>
    /// 角色方向帮助类
    /// </summary>
    public static class RoleDirectionHelper
    {
        /// <summary>
        /// 这是默认的方向识别
        /// 在游戏资源中，方向可能是数字的，也可能是汉字标识，通过一个字段数组设定图片读取规则
        /// </summary>
        public static string[] RoleDirectionStringArray = new string[]
        {
            "上","左上","左","左下","下","右下","右","右上"
        };
        public static string RoleDirectionToChinese(RoleDirection dir)
        {
            return RoleDirectionStringArray[(int)dir];
        }
        public static double GetAngleFromTowPoint(Point p1, Point p2)
        {
            var vx = p2.X - p1.X;
            var vy = p2.Y - p1.Y;
            var m = Math.Sqrt(Math.Pow(vx, 2) + Math.Pow(vy, 2));
            vx = vx / m;
            vy = vy / m;

            double angle;
            angle = Math.Acos((0 * vx) + (-1 * vy)) / Math.PI * 180;
            if (vx > 0)
                angle = 360 - angle;
            return angle;
        }
        public static RoleDirection GetRoleDirectionFromAngle(double angle)
        {
            //做了一个算法，使得方向更加正确
            int dir = (int)((angle + 22.5d) / 45.0d);
            if (dir >= 8)
                dir = 7;
            return (RoleDirection)dir;
        }
        public static RoleDirection GetRoleDirectionFormTowPoint(Point p1, Point p2)
        {
            return GetRoleDirectionFromAngle(GetAngleFromTowPoint(p1, p2));
        }
        public static RoleDirection GetMirrorDir(RoleDirection dir)
        {
            switch (dir)
            {
                case RoleDirection.Up:
                    return RoleDirection.Up;
                case RoleDirection.UpLeft:
                    return RoleDirection.UpRight;
                case RoleDirection.Left:
                    return RoleDirection.Right;
                case RoleDirection.LeftDown:
                    return RoleDirection.RightDown;
                case RoleDirection.Down:
                    return RoleDirection.Down;
                case RoleDirection.RightDown:
                    return RoleDirection.LeftDown;
                case RoleDirection.Right:
                    return RoleDirection.Left;
                case RoleDirection.UpRight:
                    return RoleDirection.UpLeft;
                default:
                    return RoleDirection.Down;
                    break;
            }
        }
    }
}
